package com.lkd.controller;

import com.lkd.dto.PartnerDto;
import com.lkd.result.PageBean;
import com.lkd.result.Result;
import com.lkd.service.PartnerService;
import com.lkd.vo.PartnerVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/user-service/partner")
public class PartnerController {
    @Autowired
    private PartnerService partnerService;

    /**
     * 添加合作商
     * @param partnerDto
     * @return
     */
    @PostMapping
    public Result savePartner(@RequestBody PartnerDto partnerDto) {
        partnerService.savePartner(partnerDto);
        return Result.success(true);
    }

    /**
     * 分页查询合作商
     * @param pageIndex
     * @param pageSize
     * @param name
     * @return
     */
    @GetMapping("/search")
    public Result pageConditonQueryPartner(
            @RequestParam(value = "pageIndex", defaultValue = "1") Integer pageIndex,
            @RequestParam(value = "pageSize", defaultValue = "10") Integer pageSize,
            @RequestParam(value = "name", required = false) String name
    ) {
        PageBean<PartnerVo> pageBean = partnerService.pageConditionQuery(pageIndex, pageSize, name);
        return Result.success(pageBean);
    }

    /**
     * 根据id查询合作商
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    public Result selectById(@PathVariable("id") Integer id) {
        return Result.success(partnerService.selectById(id));
    }

    /**
     * 修改合作商
     * @param partnerDto
     * @return
     */
    @PutMapping
    public Result updateById(@RequestBody PartnerDto partnerDto) {
        partnerService.updateById(partnerDto);
        return Result.success(true);
    }

    /**
     * 删除合作商
     * @param id
     * @return
     */
    @DeleteMapping("/{id}")
    public Result deleteById(@PathVariable("id") Integer id) {
        partnerService.deleteById(id);
        return Result.success(true);
    }

    /**
     * 查询合作商数量
     * @return
     */
    @GetMapping("/count")
    public Result count() {
        int count = partnerService.count();
        return Result.success(count);
    }
}